// components.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing components
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h"

// COMPONENT_DEFN Subtransactions
//
//////////////////////////////////////////////////////////////////////////////

//@ SEC 3
//@ CHA Component Verify
//@ COD ComponentVerify
//@ TAB COMPONENT_DEFN Read
//@ GEN Use Component Verify to verify a Component Defn record
void ComponentVerify( XCBuffer* buf, verify_type verify_mode, XCTransactionVars* vars, XCBuffer* orig ) 
{
	TRANS_INFUNC( ComponentVerify );

	//@ SUB ValidateName to check for a valid name
	ValidateName( FLD_COMPONENT_NAME, buf, ERR_COMPONENT, vars );

	//@ If verifying an ADD transaction,
	if (verify_mode == VERIFY_ADD) {
		//@ GRS

		//@ SUB LookupNumberFromName to convert component name into a component id number
		//@ ERR C (ALREADY_EXISTS) Component already exists
		if ( LookupNumberFromName( vars, vars->buffer, buf, FLD_COMPONENT_ID, FLD_COMPONENT_NAME, FMT_COMPONENT_DEFN ) )
			vars->Errors->Add( ERR_ALREADY_EXISTS, SEV_CRITICAL, FLD_COMPONENT_NAME, 1, ERR_COMPONENT );

		//@ GRE
	} else {
		//@ GRS
		
		//@ SUB ComponentRetrieve to verify that we do have a componentwith this name
		//@ ERR C (DOESNT_EXIST) Component doesn't exist
		if ( ! LookupNumberFromName( vars, vars->buffer, buf, FLD_COMPONENT_ID, FLD_COMPONENT_NAME, FMT_COMPONENT_DEFN ) || 
			!ComponentRetrieve( buf, vars ))
			vars->Errors->Add( ERR_DOESNT_EXIST, SEV_CRITICAL, FLD_COMPONENT_NAME, 1, ERR_COMPONENT );

		//@ GRE
	};

	TRANS_OUTFUNC();
};

//@ SEC 3
//@ CHA Component Change
//@ COD ComponentChange
//@ TAB COMPONENT_DEFN Write
//@ GEN Use ComponentChange to alter Component Defn records
void ComponentChange( XCBuffer* input, XCBuffer* compare, XCTransactionVars* vars ) 
{
	XCSQLGen* query = vars->query;

	TRANS_INFUNC( ComponentChange );

	//@ Issue update statement
	query->MakeFull( SQL_STMT_UPDATE, input, compare );
	vars->dbtxn->ExecuteSQL( query, SQL_NO_RESULT );

	TRANS_OUTFUNC();
};

//@ SEC 3
//@ CHA Component Retrieve
//@ COD ComponentRetrieve
//@ TAB COMPONENT_DEFN Read
//@ GEN Use ComponentRetrieve to retrieve Component Defn records
bool ComponentRetrieve( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( ComponentRetrieve );

	bool result;
	XCDBStatement* stmt = NULL;
	XCSQLGen* query = vars->query;

	try {
		XCCompare comptemp;
		query->MakeFull( SQL_STMT_SELECT, buf );
		stmt = vars->dbtxn->ExecuteSQL( query );
		result = (!stmt->Eof());
	} catch( ... ) {
		App->CleanupObjects( 1, &stmt );
		throw;
	};
	App->CleanupObjects( 1, &stmt );

	TRANS_OUTFUNCRET( result );
};
